package com.xnchat.pro.vo;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * delay通用工具类
 * @author 小凝
 */
public class DelayObject<T> implements Delayed {
    //到期时间
    private final long expire;
    //数据
    private final T t;
    //消息类别，预留，用于后续
    private int type;


    public DelayObject(long expire,  T t) {
        this.expire = expire+System.currentTimeMillis();
        this.t = t;
    }
    public DelayObject(long expire, T t, int type){
        this.expire = expire+System.currentTimeMillis();
        this.t = t;
        this.type=type;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expire - System.currentTimeMillis() , TimeUnit.MILLISECONDS);

    }

    @Override
    public int compareTo(Delayed o) {
        return this.getDelay(TimeUnit.MILLISECONDS) >= o.getDelay(TimeUnit.MILLISECONDS)?1:-1;

    }
    public T getT(){
        return this.t;
    }
    public int getType(){
        return this.type;
    }

}
